package com.forestar.mapControl.utils.mapConvert.geometry.coordinatesystem;

import com.forestar.mapControl.utils.mapConvert.geometry.GeoPoint;
import com.forestar.mapControl.utils.mapConvert.geometry.IGeometry;
import com.mapzone.api.geometry.mzGeometry;
import com.mapzone.api.geometry.mzPoint;
import com.mapzone.api.spatialreference.mzCoordinateTransform;

public class GeoTransformer
  {
    private final CoordinateSystem fromCoordinateSystem;
    private final CoordinateSystem toCoordinateSystem;
    private mzCoordinateTransform coordinateTransformWithParams;
    private mzCoordinateTransform coordinateTransformWithoutParams;
    private double[] parameters;
    private boolean isUseParameters = true;

    public GeoTransformer(CoordinateSystem from, CoordinateSystem to, EllipsoidTransParamType transParamType, double[] parameters)
      {
        // 带参转换
        this.coordinateTransformWithParams = new mzCoordinateTransform();
        coordinateTransformWithParams.setCoordinateSystem(from.getSrid(), to.getSrid(), parameters);
        // 无参数转换
        this.coordinateTransformWithoutParams = new mzCoordinateTransform();
        coordinateTransformWithoutParams.setCoordinateSystem(from.getSrid(), to.getSrid(), new double[]{0, 0, 0, 0, 0, 0, 0});
        this.fromCoordinateSystem = from;
        this.toCoordinateSystem = to;
        this.parameters = parameters;
      }

    // 设置是否使用设置的转换参数，有设置，但可以不使用
    public void setUseParameters(boolean bUse)
      {
        this.isUseParameters = bUse;
      }

    public boolean isUseParameters()
      {
        return this.isUseParameters;
      }

    private mzCoordinateTransform getCoordinateTransform()
      {
        return isUseParameters ? coordinateTransformWithParams : coordinateTransformWithoutParams;
      }

    //几何体的转换
    public boolean transform(IGeometry geometry)
      {
        if (fromCoordinateSystem.equals(toCoordinateSystem))
          {
            return false;
          }
        boolean bTrans = false;
        Object obj = geometry.getInternalObject();
        if (obj instanceof mzGeometry)
          {
            mzGeometry geo = (mzGeometry) geometry.getInternalObject();
            bTrans = getCoordinateTransform().transform(geo);
            geometry.setCoordinateSystem(CoordinateSystem.create(geo.getSrid()));
            //对单点进行特殊处理
            if (geo.getGeometryType() == mzGeometry.mzGeometryPoint)
              {
                GeoPoint point = (GeoPoint) geometry;
                mzPoint m_mzPoint = (mzPoint) geo;
                point.setX(m_mzPoint.getX());
                point.setY(m_mzPoint.getY());
              }
          }
        return bTrans;
      }

    //单点的转换{x,y}
    public boolean transform(double[] point)
      {
        return getCoordinateTransform().transform(point);
      }

    public double[] getParams()
      {
        return parameters;
      }

    public void setParameters(double[] parameters)
      {
        coordinateTransformWithParams.setCoordinateSystem(fromCoordinateSystem.getSrid(), toCoordinateSystem.getSrid(), parameters);
        this.parameters = parameters;
      }
  }
